package com.ljh.localmusic.ui.component

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.MusicNote
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.ljh.localmusic.data.local.models.AudioItem
import kotlin.math.floor

/**
 * author：LJH on 2025/8/26 09:30
 *
 */

fun convertTimestampToDuration(position: Long): String {
    val seconds = floor(position / 1E3).toInt()
    val minutes = seconds / 60
    val remainingTimeSeconds = seconds - (minutes * 60)
    return if (position < 0) "--:--" else "%d:%02d".format(minutes, remainingTimeSeconds)
}

@Composable
fun MusicItemCard(
    modifier: Modifier = Modifier,
    audioItem: AudioItem,
    onItemClick: (AudioItem) -> Unit = {}
) {
    Card(
        modifier = modifier
            .padding(horizontal = 10.dp, vertical = 6.dp)
            .clickable{
                onItemClick(audioItem)
            }
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(
                horizontal = 8.dp,
                vertical = 4.dp
            )
        ) {

            if (audioItem.artWork != null){

//                Icon(
//                    imageVector = Icons.Rounded.MusicNote,
//                    contentDescription = null,
//                    modifier = Modifier
//                        .size(40.dp)
//                        .clip(RoundedCornerShape(8.dp))
//                )

                AsyncImage(
                    model = audioItem.artWork,
                    contentDescription = null,
                    error = rememberVectorPainter(Icons.Rounded.MusicNote),
                    modifier = Modifier
                        .size(40.dp)
                        .clip(RoundedCornerShape(8.dp))
                )
            } else {
                Icon(
                    imageVector = Icons.Rounded.MusicNote,
                    contentDescription = null,
                    modifier = Modifier
                        .size(40.dp)
                        .clip(RoundedCornerShape(8.dp))
                )
            }
            Spacer(Modifier.width(4.dp))
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(
                        horizontal = 8.dp,
                        vertical = 4.dp
                    ),
                verticalArrangement = Arrangement.Center
            ) {
                Spacer(Modifier.size(4.dp))

                Text(
                    text = audioItem.displayName,
                    style = MaterialTheme.typography.titleMedium,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1
                )
                Spacer(Modifier.size(4.dp))
                Text(
                    text = audioItem.artist,
                    style = MaterialTheme.typography.bodySmall,
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1
                )
            }
            Text(
                text = convertTimestampToDuration(audioItem.duration.toLong()),
                style = MaterialTheme.typography.titleMedium,
                overflow = TextOverflow.Ellipsis,
                maxLines = 1
            )
            Spacer(modifier = Modifier.size(8.dp))

        }
    }
}